//
//  ClockDialView.m
//  UILbrary
//
//  Created by mac on 16/8/15.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ClockDialView.h"
#import "UIColor+expanded.h"

@interface ClockDialView ()
@property (nonatomic,strong) CAShapeLayer *needleLayer;
@end


@implementation ClockDialView
{
    CGFloat beginAngle;///0刻度在的弧度
    CGFloat outerRadius;///外层圆弧半径
    CGFloat outerLineWith;///外层圆弧线条宽度
    CGFloat degreeScaleNumber;///总的刻度数量
    CGFloat interRadius;///内层圆弧半径
    CGFloat textRadius;///文字圆弧半径
    CGFloat interCircleRadius;///里层圆半径
    CGFloat outerCircleRadius;///外层园半径
    CGFloat speedValue; ///速度值；
    CGFloat needleTipAngle;///表针偏移弧度（也就是速度值在表盘上的弧度）
    CGPoint needleTipPoint;///表针坐标
    CGPoint centerPoint;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
    };
    
    return self;
    
}


- (void)awakeFromNib{
    
}


- (void)initialize{
    
     beginAngle = 195;
     outerRadius = 150;
     outerLineWith = 2;
     degreeScaleNumber = 45;
     interRadius = 125;
     textRadius = 110;
     interCircleRadius = 10;
     outerCircleRadius = 20;
     speedValue = 45;///速度值；
    CGFloat needleTipAngle ;///表针偏移弧度（也就是速度值在表盘上的弧度）
    CGPoint needleTipPoint;///表针坐标
    CGPoint centerPoint;

}


- (void)drawRect:(CGRect)rect{
    
    [self drawNeedle];
    
}

- (void)animalWith:(CGFloat )value{

        //valueWithCATransform3D作用与layer
//        CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform"];
//        anima.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI/100.0, 0, 0, 1)];//绕着矢量（x,y,z）旋转
//        anima.duration = 1.0f;
//        anima.repeatCount = MAXFLOAT;
//        [self.needleLayer addAnimation:anima forKey:@"rotateAnimation"];
    
    //中心点
    [self.needleLayer setAnchorPoint:CGPointMake(1, 1)];
    CABasicAnimation *theAnimation;
    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    theAnimation.duration=8;
    theAnimation.removedOnCompletion = YES;
    theAnimation.fromValue = [NSNumber numberWithFloat:0];
    theAnimation.toValue = [NSNumber numberWithFloat:3.1415926];
    [self.needleLayer addAnimation:theAnimation forKey:@"animateTransform11"];
    
    

}

- (void)drawNeedle{
#pragma  mark - 最外层蓝色的圆弧
     centerPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    //贝斯曲线 的路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:150 startAngle:M_PI* (-195)/180  endAngle:M_PI * (15)/180 clockwise:YES];
    
    //用来画曲线
    CAShapeLayer * layer = [CAShapeLayer layer];
    //填充颜色  当设为 橘黄色的时候 如下图  设为透明为下图
    layer.fillColor = [UIColor clearColor].CGColor;
    //圆弧线条颜色
    layer.strokeColor = [UIColor colorWithHexString:@"329cd8"].CGColor;//[UIColor colorWithRed:0.12 green:0.52 blue:0.23 alpha:1].CGColor;
    //圆弧线条两端的模式，kCALineCapSquare 直角   kCALineCapRound 圆角
    layer.lineCap = kCALineCapRound;
    //圆弧线段的宽度
    layer.lineWidth = 2;
    //给layer 添加路径
    layer.path = [path CGPath];
    [self.layer addSublayer:layer];
    
    
    
    
#pragma mark - 白色的刻度
    for (int i = 0; i<45; i++) {
        
        CGFloat perAngle = 210.f/44.f;
        CGFloat startAngle = M_PI * (-195 + i * perAngle)/180;
        CGFloat textAngle = M_PI * (195 - i * perAngle)/180;
        CGFloat endAngle = startAngle + M_PI * (perAngle/5)/180;
        UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:125 startAngle:startAngle endAngle:endAngle clockwise:YES];
        CAShapeLayer *layer1 = [CAShapeLayer layer];
        layer1.fillColor = [UIColor clearColor].CGColor;
        layer1.strokeColor = [UIColor whiteColor].CGColor;
        layer1.lineCap = kCALineCapButt;//如果设为 kCALineCapSquare 则没有间距 kCALineCapRound 也会连在一起，不过会有凹凸的连续
        layer1.lineWidth = 5.0;
        if (i%4 == 0) {
            //长刻度
            layer1.strokeColor = [UIColor whiteColor].CGColor;
            layer1.lineWidth = 10.0;
            
            CGPoint point = [self calcCircleCoordinateWithCenter:centerPoint andWithAngle:textAngle andWithRadius:110];
            NSString *valueString = [NSString stringWithFormat:@"%d",i * 5];
            
            UIFont* font = [UIFont fontWithName:@"Helvetica-Bold" size:7];
            //刻度值
            UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(point.x - 5, point.y - 5, 12, 12)];
            text.text = valueString;
            text.font = font;
            text.textColor = [UIColor whiteColor];//[UIColor colorWithRed:0.54 green:0.78 blue:0.91 alpha:1.0];
            text.textAlignment = NSTextAlignmentCenter;
            [self addSubview:text];
        }
        layer1.path = [path1 CGPath];
        [self.layer addSublayer:layer1];
    }
    
    
    //表盘中心的圆弧
    //贝斯曲线 的路径
    UIBezierPath *centerCirclePath = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:20 startAngle:0  endAngle:M_PI * 2 clockwise:YES];
    
    //用来画曲线
    CAShapeLayer * CirclLayer = [CAShapeLayer layer];
    //填充颜色  当设为 橘黄色的时候 如下图  设为透明为下图
    CirclLayer.fillColor = [UIColor clearColor].CGColor;
    //圆弧线条颜色
    CirclLayer.strokeColor = [UIColor redColor].CGColor;//[UIColor colorWithRed:0.12 green:0.52 blue:0.23 alpha:1].CGColor;
    //圆弧线条两端的模式，kCALineCapSquare 直角   kCALineCapRound 圆角
    CirclLayer.lineCap = kCALineCapRound;
    //圆弧线段的宽度
    CirclLayer.lineWidth = 2;
    //给layer 添加路径
    CirclLayer.path = [centerCirclePath CGPath];
    [self.layer addSublayer:CirclLayer];
    
    
    
    //第二个表盘中心的圆弧2
    //贝斯曲线 的路径
    UIBezierPath *centerCirclePath1 = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:10 startAngle:0  endAngle:M_PI * 2 clockwise:YES];
    
    
    //用来画曲线
    CAShapeLayer * CirclLayer1 = [CAShapeLayer layer];
    //填充颜色  当设为 橘黄色的时候 如下图  设为透明为下图
    CirclLayer1.fillColor = [UIColor redColor].CGColor;
    //圆弧线条颜色
    CirclLayer1.strokeColor = [UIColor redColor].CGColor;//[UIColor colorWithRed:0.12 green:0.52 blue:0.23 alpha:1].CGColor;
    //圆弧线条两端的模式，kCALineCapSquare 直角   kCALineCapRound 圆角
    CirclLayer1.lineCap = kCALineCapRound;
    //圆弧线段的宽度
    CirclLayer1.lineWidth = 2;
    //给layer 添加路径
    CirclLayer1.path = [centerCirclePath1 CGPath];
    [self.layer addSublayer:CirclLayer1];
    
    
    
    
    
#pragma mark - 画表针
    UIBezierPath *needlePath = [UIBezierPath bezierPath];
    [needlePath moveToPoint:centerPoint];
    CGFloat needleWidth = 10;
    CGFloat needleHeight = 120;
    //needleWidth needleWith
    
    //表尖坐标  只要控制好表尖坐标 则这个表针就可以动了
    if (self.speedValue && self.speedValue != speedValue) {
        
        speedValue = self.speedValue;
    }else{
        speedValue = 45;
    }
    
    
    //根据速度算出此速度在表盘上面的弧度
     needleTipAngle = (195- speedValue/5 * 210.0/44.0) /180.0 * M_PI;//195/180.0 * M_PI
     needleTipPoint = [self calcCircleCoordinateWithCenter:centerPoint andWithAngle:needleTipAngle andWithRadius:needleHeight];
    [needlePath addLineToPoint:CGPointMake(centerPoint.x - needleWidth, centerPoint.y)];
    [needlePath addLineToPoint:needleTipPoint];
    [needlePath addLineToPoint:CGPointMake(centerPoint.x + needleWidth, centerPoint.y)];
        [needlePath closePath];
    //    [[UIColor redColor] setFill];
    
    
    //    [needlePath fill];
    _needleLayer = [CAShapeLayer layer];
    _needleLayer.fillColor = [UIColor redColor].CGColor;
    _needleLayer.strokeColor = [UIColor redColor].CGColor;
    _needleLayer.lineCap = kCALineCapRound;
    _needleLayer.lineWidth = 0;
    _needleLayer.path = [needlePath CGPath];
    [self.layer addSublayer:_needleLayer];
}

- (CGPoint) calcCircleCoordinateWithCenter:(CGPoint) center  andWithAngle : (CGFloat) angle andWithRadius: (CGFloat) radius{
    CGFloat x2 = radius*cosf(angle);
    CGFloat y2 = radius*sinf(angle);
    return CGPointMake(center.x+x2, center.y-y2);
    
}
@end
